package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Link;
import com.itheima.pojo.Tag;
import com.itheima.pojo.WebConfig;
import com.itheima.service.BlogService;
import com.itheima.service.LinkService;
import com.itheima.service.WebConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private LinkService linkService;
    @Autowired
    private BlogService blogService;

    @GetMapping("/addLinkCount")
    public Result addLinkCount(@RequestParam("uid") String uid){
        boolean flag = linkService.addLinkCount(uid);
        return new Result("success","增加链接点击数成功");
    }

    @GetMapping("/getBlogByLevel")
    public Result getBlogByLevel(@RequestParam("level") Integer level,@RequestParam("currentPage") Long currentPage,@RequestParam("useSort") Integer useSort){
        Page<Blog> page = blogService.getBlogByLevel(level,currentPage,useSort);
        Map data = new HashMap();
        data.put("records",page.getResult());
        data.put("size",page.getPageSize());
        data.put("total",page.getTotal());
        data.put("current",currentPage);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        return new Result("success",data);
    }

    @GetMapping("/getBlogByTime")
    public Result getBlogByTime (@RequestParam("currentPage") Long currentPage,@RequestParam("pageSize") Long pageSize){
        Page<Blog> page = blogService.getBlogByTime(currentPage,pageSize);
        Map data = new HashMap();
        data.put("records",page.getResult());
        data.put("size",page.getPageSize());
        data.put("total",page.getTotal());
        data.put("current",currentPage);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        return new Result("success",data);
    }

    @GetMapping("/getHotBlog")
    public Result getHotBlog (){
        List<Blog> page = blogService.getHotBlog();
        Map data = new HashMap();
        data.put("records",page);
        data.put("size",10);
        data.put("total",page.size());
        data.put("current",1);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        return new Result("success",data);
    }

    @GetMapping("/getHotTag")
    public Result getHotTag (){
        List<Tag> list = blogService.getHotTag();
        Map data = new HashMap();
        data.put("records",list);
        data.put("size",10);
        data.put("total",list.size());
        data.put("current",1);
        data.put("optimizeCountSql",true);
        data.put("isSearchCount",true);
        return new Result("success",data);
    }
    @GetMapping("/getLink")
    public Result getLink (){
        List<Link> list = linkService.getLink();
        return new Result("success",list);
    }

    @GetMapping("/getNewBlog")
    public Result getNewBlog(@RequestParam("currentPage") Long currentPage,@RequestParam("pageSize") Long pageSize){
        List<Blog> list = blogService.getNewBlog(currentPage,pageSize);
        return new Result("success",list);
    }

    @Autowired
    private WebConfigService webConfigService;
    @GetMapping("/getWebConfig")
    public Result getWebConfig (){
        List<WebConfig> list = webConfigService.findWebConfig();
        return new Result("success",list);
    }
}
